package com.xingchi.unique.config;

import com.xingchi.unique.factory.RedisIdFactory;
import com.xingchi.unique.factory.SnowflakeFactory;
import com.xingchi.unique.factory.UUIDFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * 生成工厂配置
 *
 * @author xingchi
 * @date 2023/3/10 18:54
 * @modified xingchi
 */
@Configuration
@EnableConfigurationProperties({UniqueProperties.class})
public class GenerateFactoryConfig {

    @Bean
    @DependsOn("stringRedisTemplate")
    @ConditionalOnMissingBean
    public RedisIdFactory redisIdFactory(StringRedisTemplate stringRedisTemplate) {
        return new RedisIdFactory(stringRedisTemplate);
    }

    @Bean
    @ConditionalOnMissingBean
    public UUIDFactory uuidFactory() {
        return new UUIDFactory();
    }

    @Bean
    @ConditionalOnMissingBean
    public SnowflakeFactory snowflakeFactory(UniqueProperties uniqueProperties) {
        UniqueProperties.Snowflake snowflake = uniqueProperties.getSnowflake();
        return new SnowflakeFactory(snowflake.getWorkId(), snowflake.getDatacenterId());
    }

}
